package controller;
import entity.Staff;
import java.io.IOException;
import javax.ejb.EJB;
import javax.persistence.NoResultException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.StaffFacade;


/**
 *
 * @author Declan
 */
@WebServlet(name="LoginServlet", loadOnStartup = 1, urlPatterns={"/login", "/logout"})
public class LoginServlet extends HttpServlet {

    @EJB
    private StaffFacade staffFacade;

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        // Process request (Perform actions, set response attributes, set path for forward dispatch)
        String servletPath = request.getServletPath();
        String fdPath = null;
        
        if(servletPath.equals("/login")) {
            HttpSession session = request.getSession();
            request.setAttribute("sessionID", session.getId());
            fdPath = "/WEB-INF/view/login/default.jsp";
        }
        else if(servletPath.equals("/logout")) {
            HttpSession session = request.getSession(false);
            if(session != null)
                session.invalidate();
            fdPath = "/WEB-INF/view/login/default.jsp";
        }

        // Forward dispatch
        if(fdPath == null)
            fdPath = "/WEB-INF/jspf/error/404.jsp";

        request.getRequestDispatcher(fdPath).forward(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        // Process request (Perform actions, set response attributes, set path for forward dispatch)
        String servletPath = request.getServletPath();
        String fdPath = null;

        if(servletPath.equals("/login")) {
            HttpSession session = request.getSession();
            request.setAttribute("sessionID", session.getId());

            String enteredUsername = request.getParameter("username");
            String enteredPassword = request.getParameter("password");

            fdPath = "/WEB-INF/view/login/default.jsp";

            try {
                Staff user = (Staff)staffFacade.queryFindStaffByUsername(enteredUsername).get(0);

                if(enteredPassword.equals(user.getPassword()))
                {
                    session.setAttribute("loggedIn", true);
                    session.setAttribute("username", enteredUsername);
                    
                    fdPath = "/index.jsp";
                }
                else
                    request.setAttribute("incorrectLogin", true);
            } catch(Exception ex) {
                request.setAttribute("incorrectLogin", true);
            }

            
        }
        else if(servletPath.equals("/logout")) {
            HttpSession session = request.getSession(false);
            if(session != null)
                session.invalidate();
            fdPath = "/WEB-INF/view/login/default.jsp";
        }


        // Forward dispatch
        if(fdPath == null)
            fdPath = "/WEB-INF/jspf/error/404.jsp";

        request.getRequestDispatcher(fdPath).forward(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Servlet to handle all requests with URL /login/*";
    }

}
